<template>
  <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-table border v-loading="loading" :data="dataList" show-summary :summary-method="getSummaries"
                ref="table"
                :max-height="tableMaxHeight">
        <el-table-column type="index" align="center" label="序号"></el-table-column>
        <el-table-column label="到货类型" align="center" prop="fahuoType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fahuo_type" :value="scope.row.fahuoType"/>
          </template>
        </el-table-column>
        <el-table-column label="采购单号" align="center" prop="goodsSalebuyall.caigouno"/>
        <el-table-column label="到货单号" align="center" prop="lvDaohuo.nos"/>
        <el-table-column label="请购单号" align="center" prop="goodsSalebuyall.nos"/>
        <el-table-column label="供应商" align="center" prop="supplierName" show-overflow-tooltip/>
        <el-table-column label="订单编号" align="center" prop="projectNo" show-overflow-tooltip/>
        <el-table-column label="客户名称" align="center" prop="goodsSalebuyall.customerName" show-overflow-tooltip/>
        <el-table-column label="要货单位" align="center" prop="goodsSalebuyall.projectName" show-overflow-tooltip/>
        <el-table-column label="制单人" align="center" prop="createBy" show-overflow-tooltip/>
        <el-table-column label="需求日期" align="center" prop="goodsSalebuyall.saleDate"/>
        <el-table-column label="中标价" align="center" prop="goodsSalebuyall.goodsUnitPrice"/>
        <el-table-column label="产品编号" align="center" prop="goodsSalebuyall.goodsCode" show-overflow-tooltip/>
        <el-table-column label="产品名称" align="center" prop="goodsSalebuyall.goodsName" show-overflow-tooltip/>
        <el-table-column label="产品型号" align="center" prop="goodsSalebuyall.goodModel"/>
        <el-table-column label="送货人" align="center" prop="lvDaohuo.songhuoren"/>
        <el-table-column label="到货时间" align="center" prop="lvDaohuo.arrivalDate"/>
        <el-table-column label="到货数量" align="center" prop="daohuoCount"/>
        <el-table-column label="到货重量" align="center" prop="daohuoDun"/>
        <el-table-column label="到货单价" align="center" prop="unitPrice"/>
        <el-table-column label="到货金额" align="center" prop="totalPrice"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-set-up"
                       @click="qualityTestingDaohuo(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">到货质检
            </el-button>
<!--            <el-button size="mini" type="text" icon="el-icon-set-up" v-if="scope.row.goods.type === '0'"-->
<!--                       @click="qualityTesting(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">产品质检-->
<!--            </el-button>-->
<!--            <el-button size="mini" type="text" icon="el-icon-set-up"-->
<!--                       @click="qualityMaterialTesting(scope.row)" v-hasPermi="['develop:LvDaohuoDetail:edit']">材料质检-->
<!--            </el-button>-->
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>
    <factory-test-quality ref="qualityTesting" @ok="getList"> </factory-test-quality>
    <factory-test-quality-all ref="qualityTestingAll" @ok="getList"> </factory-test-quality-all>
    <factory-test-quality-daohuo ref="qualityTestingDaohuo" @ok="getList"> </factory-test-quality-daohuo>
  </el-dialog>
</template>

<script>
import {accAdd, deleteAction, getAction} from '@/api/manage'
import LvDaohuoDetailForm from '@/views/develop/lvDaohuoDetail/module/LvDaohuoDetailForm'
import FactoryTestQualityAll from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityAll.vue";
import FactoryTestQuality from "@/views/develop/factory/factoryTestQuality/FactoryTestQuality.vue";
import FactoryTestQualityDaohuo from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityDaohuo.vue";

export default {
  dicts: [ 'fahuo_type'],
  name: "LvDaohuoDetailList",
  components: {
    FactoryTestQualityDaohuo,
    FactoryTestQuality, FactoryTestQualityAll
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "到货明细表",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 到货明细表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        saleId: null,
        daohuoId: null,
        goodId: null,
        daohuoCount: null,
        orderByColumn: "a.good_id",
        isAsc: "asc",
      },

      url: {
        list: '/lvDaohuoDetail/lvDaohuoDetail/list',
        remove: '/lvDaohuoDetail/lvDaohuoDetail/',
        export: '/lvDaohuoDetail/lvDaohuoDetail/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询到货明细列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    showDaohuoDetailList(saleId,goodId) {
      this.open = true;
      this.queryParams.saleId = saleId;
      this.queryParams.goodId = goodId;
      this.getList();
    },

    showDaohuoDetailListByDaohuoId(daohuoId) {
      this.open = true;
      this.queryParams.daohuoId = daohuoId;
      this.getList();
    },

    showDaohuoDetailListByDaohuo(row) {
      this.open = true;
      this.title = "到货明细表（"+row.nos+"）";
      this.queryParams.daohuoId = row.id;
      this.getList();
    },

    showDaohuoDetailListByDaohuoNos(row) {
      this.open = true;
      this.title = "到货明细表（"+row.lvDaohuo.nos+"）";
      this.queryParams.daohuoId = row.daohuoId;
      this.getList();
    },


    qualityTestingDaohuo(row){
      if(row.lvDaohuo.status==='0'){
        this.$refs.qualityTestingDaohuo.showByDaohuoDetail(row,true);
      }else{
        // this.$refs.qualityTestingDaohuo.showByDaohuoDetail(row,false);
        this.$refs.qualityTestingDaohuo.showByDaohuoDetail(row,true);
      }
    },


    qualityMaterialTesting(row){
      this.$refs.qualityTestingAll.showByDaohuoDetail(row);
    },



    qualityTesting(row){
      this.$refs.qualityTesting.showByDaohuoDetail(row);
    },

    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "daohuoCount" || column.property === "totalPrice" || column.property === "goodsSalebuyall.goodsUnitPrice" || column.property === "daohuoDun" ||  column.property === "unitPrice") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },




  }
};
</script>
